﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Caching;

namespace Utility.Web
{    
    public class WebCache
    {
        public static object Get(string key)
        {
            return HttpContext.Current.Cache[key];
        }
        public static void Set(string key, object value, int cacheTimeInSeconds)
        {
            HttpContext.Current.Cache.Add(key, value, null, DateTime.Now.AddSeconds(cacheTimeInSeconds), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
        }
        public static void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        public static List<string> GetListCache()
        {
            List<string> keys = new List<string>();

            IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
            while (enumerator.MoveNext())
                keys.Add(enumerator.Key.ToString());

            return keys;
        }

        public static void FlushCache()
        {
            List<string> lstCache = GetListCache();

            foreach (string cache in lstCache)
                Remove(cache);
        }
    }
}
